package com.sogo;


import android.content.ContentValues;   
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;


public class DBAdapter {
	public static final String KEY_ROWID = "id";
	public static final String KEY_USRNAME = "username";
	public static final String KEY_FIRSTNAME = "firstname";
	public static final String KEY_LASTNAME = "lastname";
	public static final String KEY_EMAIL = "email";
	public static final String KEY_PASSWORD = "password";
	public static final String KEY_REFRESHRATE="refreshrate";
	public static final String KEY_ENABLELOCATION="enabledLocation";
	public static final String KEY_AVATARPATH="avatarpath";
	private static final String TAG = "DBAdapter";
	
	private static final String DATABASE_NAME = "UserDatabase";
	private static final String DATABASE_TABLE = "UserInfo";
	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_CREATE = 
			"create table UserInfo (id integer primary key not null, "
			+ "username text not null, firstname text not null, lastname text not null, " +
			"email text not null, password text not null, refreshrate integer not null, enabledLocation integer not null, avatarpath text not null);";
	
	private final Context context;
	
	private DatabaseHelper DBHelper;
	private SQLiteDatabase database;
	
	public DBAdapter(Context ctx)
	{
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		public void onCreate(SQLiteDatabase database)
		{
			Log.w(TAG, "Upgrading database from version  to , which will destroy all old data");
			try{
				database.execSQL(DATABASE_CREATE);
			}catch (SQLException e){
				e.printStackTrace();
			}
		}
		
		public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion)
		{
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + 
					", which will destroy all old data");
			database.execSQL("DROP TABLE IF EXISTS UserInfo");
			onCreate(database);
		}
	}
		public DBAdapter open() throws SQLException
		{
			database = DBHelper.getWritableDatabase();
			return this;
		}
		public String getOwnID()
		{
			String userID=null;
			open();
			Cursor c = getUser();
			if(c.moveToFirst())
			{
				userID=c.getString(0);	//grab your own id
			}

			close();
			return userID;
			
		}
		public String getRefreshRate()
		{
			String refreshRate=null;
			open();
			Cursor c = getUser();
			if(c.moveToFirst())
			{
				refreshRate=c.getString(6);	//grab the refresh rate
			}

			close();
			return refreshRate;
			
		}
		public void updateRefreshRate(int rowid,int value)
		{
			open();
			ContentValues args = new ContentValues();
			args.put(KEY_REFRESHRATE, value);
			database.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowid, null);
			
			
			
			close();
		}		
		public void updateAvatar(int rowid,String path)
		{
			open();
			ContentValues args = new ContentValues();
			args.put(KEY_AVATARPATH, path);
			database.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowid, null);
			
			
			
			close();
		}		
		public String avatarPath()
		{
			String path=null;
			open();
			Cursor c = getUser();
			if(c.moveToFirst())
			{
				path=c.getString(8);	//grab the imagepath
			}
			close();
			return path;
			
		}
		public String getEnableLocation()
		{
			String enabledLocation=null;
			open();
			Cursor c = getUser();
			if(c.moveToFirst())
			{
				enabledLocation=c.getString(7);	//grab the refresh rate
			}
			close();
			return enabledLocation;
			
		}
		public void updateEnableLocation(int rowid,int value)
		{
			open();
			ContentValues args = new ContentValues();
			args.put(KEY_ENABLELOCATION, value);
			database.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowid, null);
			close();
		}
		public void close()
		{
			DBHelper.close();
		}
		
		public long insertUser(int id,String username, String firstname, String lastname, String email, String password, String avatar)
		{
			ContentValues initialValues = new ContentValues();
			//existRefresh=getRefreshRate(); //crashes for some unknown reason.
			int number=0;
				number=3600000;//60 minutes default
			int enableFlag = 1;
			
			initialValues.put(KEY_ROWID,id);
			initialValues.put(KEY_USRNAME, username);
			initialValues.put(KEY_FIRSTNAME, firstname);
			initialValues.put(KEY_LASTNAME, lastname);
			initialValues.put(KEY_EMAIL, email);
			initialValues.put(KEY_PASSWORD, password);
			initialValues.put(KEY_REFRESHRATE, number);
			initialValues.put(KEY_ENABLELOCATION, enableFlag);
			initialValues.put(KEY_AVATARPATH, avatar);
			return database.insert(DATABASE_TABLE, null, initialValues);
		}
		
		public boolean updateUser(long rowid, String username, String firstname, String lastname, String email, String password)
		{
			ContentValues args = new ContentValues();
			args.put(KEY_USRNAME, username);
			args.put(KEY_FIRSTNAME, firstname);
			args.put(KEY_LASTNAME, lastname);
			args.put(KEY_EMAIL, email);
			args.put(KEY_PASSWORD, password);
			return database.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowid, null) > 0;
		}
		
		public Cursor getUser()
		{
			return database.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_USRNAME, KEY_FIRSTNAME, KEY_LASTNAME, KEY_EMAIL, KEY_PASSWORD,KEY_REFRESHRATE,KEY_ENABLELOCATION,KEY_AVATARPATH}, null, null, null, null, null);
		}
		
		public int deleteDB()
		{
			//database.execSQL("DROP TABLE IF EXISTS DATABASE_TABLE");
			return database.delete(DATABASE_TABLE,null,null);
		}
		

}
